package tests.delete;

import static org.junit.Assert.*;

import model.Budget;
import model.BudgetFactory;
import model.Line;
import model.Subline;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

/*
 * 
 * Attempts to delete a budget with an existing line and subline
 * 
 */

public class SublineDeleteTest01
{
	Budget budget;
	Line line;
	Subline subline;
	BudgetFactory factory;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
	}

	@Before
	public void setUp() throws Exception
	{
		factory = new BudgetFactory();
		
		//Create a budget
        budget = (Budget) factory.create();
        budget.setName("Test budget");
        
        budget.setDescription("This is a delete test budget.");
        
        line = (Line) budget.createLine();
        budget.add(line);
        
        subline = (Subline) line.createSubline();
        subline.setName("Subline delete test.");
		line.add(subline);
        
        budget.commit();
	}

	@After
	public void tearDown() throws Exception
	{
		Helper.delete(line, "Lines", WorkletContext.getInstance());
		Helper.delete(budget, "Budgets", WorkletContext.getInstance());
	}

	@Test
	public void test()
	{
	 	 try
		 {
            line.delete(subline);
            
            Boolean successful = budget.commit();
            
            if(!successful)
            {
            	fail("Failed to delete budget with line and subline");
            }            
        }
        catch(Exception e)
        {
            fail("Deleting failed with exception: " + e);
        }
	}
}